package com.ylyq.ylxx.controller;

import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.OceanengineGetTetrisSite;
import com.ylyq.ylxx.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @ClassName FileController
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/9 13:44
 * @Version 1.0
 */
@RestController
@RequestMapping("/file")
@Api(description = "文件上传")
public class FileController {

    @Autowired
    private IFileService fileService;

    @PostMapping("/uploadPhotoOrFile")
    @ApiOperation(value = "文件上传")
    public ResponseData uploadPhotoOrFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
//        try {
//            InetAddress addr = InetAddress.getLocalHost();
//            String ip = addr.getHostAddress();
//            if (ip.startsWith("192.168")){
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                String realPath = request.getServletContext().getRealPath("/");
//                String contextPath = request.getContextPath();
//                // 判断是本地，还是域名
//                boolean resultIps = Tools.isIP(request.getServerName());
//                System.out.println("是不是ip：" + resultIps);
//                String basePath  = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
//                        + contextPath + "/"; // 《获取本地访问地址，有项目名称》
//                int i=0;
//            }else {
////                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
////                com.jeeplus.common.utils.FileUtils.createDirectory("/soft/tomcat/apache-tomcat-8.5.32/webapps/pic/" + dateStr + "/" + uuid);
////                System.out.println("服务器上建立文件夹的路径---------" + path + dateStr);
////                uploadFile.transferTo(new File("/soft/tomcat/apache-tomcat-8.5.32/webapps/pic/" + dateStr + "/" + uuid + "/" + uploadFile.getOriginalFilename()));
////                System.out.println("文件路径------------" + path + dateStr + "/" + uploadFile.getOriginalFilename());
////
////                uploadfilename = path + dateStr + "/" + uuid + "/" + uploadFile.getOriginalFilename();
//            }
//        }catch (Exception e){
//
//        }

        try {
            String fileUrl = fileService.uploadPhotoOrFile(file);
            if (StringUtils.isBlank(fileUrl))
                return new ResponseData(ResponseData.Result.FAIL);
            return new ResponseData(ResponseData.Result.SUCCESS, fileUrl);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return new ResponseData(ResponseData.Result.FAIL);
        }
    }


    @PostMapping("/uploadPhotoOrFiles")
    @ApiOperation(value = "文件批量上传")
//    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "content-type", value = "multipart/form-data", required = true) })
    public ResponseData uploadPhotoOrFiles(@RequestParam("files") MultipartFile[] files) {
        try {
            List<String> resultsUrl = fileService.uploadPhotoOrFiles(files);
            if (resultsUrl.isEmpty())
                return new ResponseData(ResponseData.Result.FAIL);
            return new ResponseData(ResponseData.Result.SUCCESS, resultsUrl);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return new ResponseData(ResponseData.Result.FAIL);
        }
    }
}
